/*
 * @Author: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @Date: 2025-07-01 12:03:47
 * @LastEditors: 无聊 7989314+Hwz1997@user.noreply.gitee.com
 * @LastEditTime: 2025-09-03 02:39:19
 * @FilePath: \keep_motion_api\models\product\logistic.model.js
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
const mongoose = require('mongoose');
const Schema = mongoose.Schema;

// 物流模型 
const logisticSchema = new Schema({
  order_id: { 
    type: Schema.Types.ObjectId, 
    ref: 'Order',
    required: true 
  },
  tracking_number: String,
  carrier: String,
  status: String,
  history: [{
    status: String,
    location: String,
    timestamp: Date
  }],
  estimated_delivery: Date,
  delivery_address: {
    recipient: String,
    phone: String,
    province: String,
    city: String,
    district: String,
    detail: String
  },
  insurance: {
    insured_amount: Number,
    provider: String
  }
});

logisticSchema.methods.getLogistics = async function() {
  return await this.model('Logistic').find();
}
logisticSchema.methods.updateLogistics = async function(orderId, data) {
  return await this.model('Logistic').findByIdAndUpdate(orderId, data, { new: true });
}
logisticSchema.methods.addTracking = async function(orderId, data) {
  return await this.model('Logistic').findByIdAndUpdate(orderId, { $push: { history: data } }, { new: true });
}
logisticSchema.methods.getDelayed = async function(orderId) {
  return await this.model('Logistic').findById(orderId);
}
module.exports = mongoose.model('Logistic', logisticSchema);
